<template>
  <div class="list-content">
    <el-row type="flex" justify="space-between">
      <el-form :inline="true" :model="formInline" class="serach-form" @submit.native.prevent>
        <el-form-item v-if="isMuilUnitUser">
          <el-select filterable @change="getList(1)" v-model="formInline.organizationId" placeholder="请选择业主单位">
            <el-option
              v-for="item in orgOptions"
              :key="item.organizationId"
              :label="item.shortName"
              :value="item.organizationId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input 
          v-model.trim="formInline.name" 
          placeholder="请输入小微场所名称"
          @keyup.enter.native="getList(1)"
          clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="getList(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns">
          <el-button type="primary" @click="addPlan()">新增</el-button>
      </div>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="mainTable"
      style="width: 100%">
      <el-table-column
      label="序号"
      type="index"
      width="50">
      </el-table-column>
      <el-table-column
      prop="name"
      label="小微场所名称">
      </el-table-column>
      <el-table-column
      prop="location"
      label="位置">
      </el-table-column>
      <el-table-column
      prop="tel"
      label="联系电话">
      </el-table-column>
      <el-table-column
      prop="createAccountName"
      label="创建人">
      </el-table-column>
      <el-table-column
      prop="createTime"
      label="创建时间">
      </el-table-column>
      <el-table-column 
      label="操作"
      width="140">
        <template slot-scope="scope">
          <el-button  @click="showDetail(scope.$index, scope.row)" type="text" size="small">详情</el-button>
          <span class="dividing-line">|</span>
          <el-button  @click="addPlan(scope.$index, scope.row)" type="text" size="small">修改</el-button>
          <span class="dividing-line">|</span>
          <el-button  @click="delPlan(scope.$index, scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
      <!-- 展开详情 -->
      <el-table-column type="expand" width="1">
        <template slot-scope="scope">
          <div class="detail-content">
            <span style="display:none;">{{freashFlag}}</span> 
            <p>
              <b>关联用户：</b>
              <span v-if="detailObj[scope.row.id] && detailObj[scope.row.id].userList && detailObj[scope.row.id].userList.length>0"><span v-for="(item, i) in detailObj[scope.row.id].userList" :key="i">{{item?item.name:''}}&nbsp;&nbsp;&nbsp;&nbsp;</span></span>
              <span v-else>暂无数据</span>
            </p>
            <p>
              <b>关联视频：</b>
              <span v-if="detailObj[scope.row.id] && detailObj[scope.row.id].videoList && detailObj[scope.row.id].videoList.length>0"><span v-for="(item, i) in detailObj[scope.row.id].videoList" :key="i">{{item?item.name:''}}&nbsp;&nbsp;&nbsp;&nbsp;</span></span>
              <span v-else>暂无数据</span>
            </p>
            <p>
              <b>关联的物联设备：</b>
              <span v-if="detailObj[scope.row.id] && detailObj[scope.row.id].fcfsList && detailObj[scope.row.id].fcfsList.length>0"><span v-for="(item, i) in detailObj[scope.row.id].fcfsList" :key="i">{{item?item.name:''}}&nbsp;&nbsp;&nbsp;&nbsp;</span></span>
              <span v-else>暂无数据</span>
            </p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="formInline.currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.currentPageTotal">
    </el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="小微场所维护"
      width="600px"
      top='10vh'
      v-if="dialogFalg"
      :visible.sync="dialogFalg"
      @close="closeDialog"
      v-dialogDrag>
      <edit-form :editInfo="editInfo" :closeDialog="closeDialog"></edit-form>
    </el-dialog>
  </div>
</template>

<script>
import { selectNineSmallVenues,selectVenuesRelateInfo,deleteNineSmallVenues} from '@/api/venues'
import { getOrgList} from '@/api/org'
import util from '@/utils/util'
import editForm from './Dialog/NineSmallVenuesForm'

export default {
  name: 'NineSmallVenuesSet',
  props: {
    
  },
  components: { 
    editForm
  },
  data () {
    return {
      freashFlag:0,
      formInline: {
        organizationId:'',
        name:'',
        currentPageSize: 10,
        currentPageIndex: 1,
        currentPageTotal: 0
      },
      util:util,
      isMuilUnitUser:this.$store.state.userinfo.isMuilUnitUser,
      accountName:this.$store.state.userinfo.accountName,
      defaultImg: this.GLOBAL.defaultImg,
      tableData: [],//表格数据
      detailObj:{},//展开详情
      loading: false,
      orgOptions: [{organizationId: '',shortName: "全部业主单位"}],
      dialogFalg: false,
      editInfo: {  }// 编辑对应的表单信息
    }
  },
  created:function() {
    //绑定下拉框
    getOrgList({queryType: 0}).then((data)=>{
      this.orgOptions.push.apply(this.orgOptions,data.data);
      /* if(this.orgOptions.length>0){
        this.formInline.organizationId = this.orgOptions[0].organizationId
      } */
      this.getList()
    })  
  },
  methods: {
    sizeChange(_pageSize) {//页码切换
      this.getList(1,_pageSize) 
    },
    pageChange(_pageIndex) {//翻页
      this.getList(_pageIndex) 
    },
    getList(_pageIndex, _pageSize) {//获取列表
      if(_pageIndex){
        this.formInline.currentPageIndex = _pageIndex
      }
      if(_pageSize){
          this.formInline.currentPageSize = _pageSize
      }
      this.loading =true;
      selectNineSmallVenues({
        queryType:1,
        organizationId: this.formInline.organizationId,
        pageIndex: this.formInline.currentPageIndex -1,
        pageSize: this.formInline.currentPageSize,
        name: this.formInline.name.trim(),
      }).then((result) => {
        this.formInline.currentPageTotal = result.total;
        this.tableData = result.data;
        this.loading =false;
      }) 
    },
    showDetail(index, row) {//展开详情
      const $table = this.$refs.mainTable
      row.isExpand = !row.isExpand;//给row一个自定义展开状态
      $table.toggleRowExpansion(row)
      if(row.isExpand){
        this.loading =true;
        selectVenuesRelateInfo({
          venuesId : row.id
        }).then((result) => {
          this.detailObj[row.id] = result.data
          this.freashFlag++
          this.loading =false
        })
      }
    },
    addPlan(index, row) {
      this.dialogFalg = true
      this.editInfo = row||{organizationId:this.formInline.organizationId}
    },
    delPlan(index, row) {
      let that = this
      this.$confirm("确定删除？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        roundButton: true
      }).then(() => {
        deleteNineSmallVenues({id: row.id}).then(res => {
          if( res.status === 200) {
            that.$message({
              type: 'success',
              message: '删除成功!'
            })
            that.getList(1)
          }else{
            that.$message.error(res.info)
          }
        })
      }).catch(() => {

      })
    },
    closeDialog(refresh) {
      this.dialogFalg = false
      if(refresh) {
        this.getList(this.editInfo.id?'':1)
      }
    },
  },
}
</script>